home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
10,000 Great Games
/
10,000 Great Games.iso
/
Product
/
66
/
data1.cab
/
Source_Files
/
Src
/
Mouse.cpp
< prev
next >
Wrap
C/C++ Source or Header
|
2000-01-16
|
6KB
|
303 lines
#include "stdafx.h"
int reverse_mouse;
static LPDIRECTINPUTDEVICE2 mouse = 0;
static DIMOUSESTATE mousestate;
void init_mouse()
{
mouse = create_input_device_mouse();
acquire_mouse();
}
void acquire_mouse()
{
#ifndef _DEBUG
mouse->Acquire();
#endif
}
void deinit_mouse()
{
if (mouse != 0)
{
mouse->Unacquire();
mouse->Release();
}
}
LPDIRECTINPUTDEVICE2 create_input_device_mouse()
{
LPDIRECTINPUTDEVICE2 mouse;
mouse = create_input_device(GUID_SysMouse);
if (FAILED(mouse->SetCooperativeLevel(mainwindowhandle, DISCL_EXCLUSIVE | DISCL_FOREGROUND)))
error("Unable to set cooperative level for mouse");
if (FAILED(mouse->SetDataFormat(&c_dfDIMouse)))
error("Unable to set data format for mouse");
DIPROPDWORD dipdw;
dipdw.diph.dwSize = sizeof(DIPROPDWORD);
dipdw.diph.dwHeaderSize = sizeof(DIPROPHEADER);
dipdw.diph.dwObj = 0;
dipdw.diph.dwHow = DIPH_DEVICE;
dipdw.dwData = MOUSE_BUFFERSIZE;
if (FAILED(mouse->SetProperty(DIPROP_BUFFERSIZE, &dipdw.diph)))
error("Unable to set mouse buffer size");
return mouse;
}
cMouse::cMouse()
{
// Set image
set_image(in_game_pointer);
// Reset mouse state
reset();
}
cMouse::~cMouse()
{
}
void cMouse::reset()
{
// Reset mickey counters
mx = 0, my = 0;
// Update mouse buttons
bl_last = 0;
bl_double = FALSE;
}
void cMouse::determine(cPlayer *p)
{
// Get the state of the mouse
if (FAILED(mouse->GetDeviceState(sizeof(mousestate), &mousestate)))
{
// Mouse input was lost, reacquire
mouse->Acquire();
if (FAILED(mouse->GetDeviceState(sizeof(mousestate), &mousestate)))
return;
}
// Compute new x and y mickey count
mx += mousestate.lX, my += mousestate.lY;
if (mx < -MICKEYS_XMAX)
mx = -MICKEYS_XMAX;
else if (mx > MICKEYS_XMAX)
mx = MICKEYS_XMAX;
if (my < -MICKEYS_YMAX)
my = -MICKEYS_YMAX;
else if (my > MICKEYS_YMAX)
my = MICKEYS_YMAX;
// Compute new button state
int bl = mousestate.rgbButtons[reverse_mouse? 1:0] & 0x80,
br = mousestate.rgbButtons[reverse_mouse? 0:1] & 0x80;
// Get the state in a buffer for checking doubleclicks
DIDEVICEOBJECTDATA dod[MOUSE_BUFFERSIZE];
DWORD items = MOUSE_BUFFERSIZE;
if (FAILED(mouse->GetDeviceData(sizeof(DIDEVICEOBJECTDATA), dod, &items, 0)))
{
// We lost mouse input, reacquire
mouse->Acquire();
if (FAILED(mouse->GetDeviceData(sizeof(DIDEVICEOBJECTDATA), dod, &items, 0)))
{
// Unable to reacquire, reset button info
bl_last = 0;
bl_double = FALSE;
return;
}
}
for (DWORD d = 0; d < items; d++)
{
// Check if this means left button is pressed
if ((reverse_mouse? dod[d].dwOfs == DIMOFS_BUTTON1 : dod[d].dwOfs == DIMOFS_BUTTON0) && (dod[d].dwData & 0x80))
{
if (dod[d].dwTimeStamp - bl_last <= MOUSE_DCLICKTIME)
{
// This is a double click
bl_last = 0;
bl_double = TRUE;
}
else
{
// It's not a double click, but remember last time this key was pressed
bl_last = dod[d].dwTimeStamp;
}
}
}
// Make discrete states
int half_left = mx < -MICKEYS_XHALF,
half_right = mx > MICKEYS_XHALF,
is_up = my < -MICKEYS_YFULL,
is_down = my > MICKEYS_YFULL,
is_left = mx < -MICKEYS_XFULL,
is_right = mx > MICKEYS_XFULL,
confirmed_up = is_up && bl,
confirmed_down = is_down && bl,
confirmed_left = is_left && bl,
confirmed_right = is_right && bl;
// Set mouse pointer
make_dirty();
set_position(p->x + mx * 50 / MICKEYS_XMAX, p->y + 20 - my * 50 / MICKEYS_YMAX);
make_dirty();
// Cannot do anything when not active
if (!p->is_active())
return;
// Check fire button
if (br)
{
if (is_up && !is_left && !is_right)
p->fire_up();
else if (is_down && !half_left && !half_right)
p->fire_down();
else
p->fire();
}
// Cannot do anything more when captured
if (p->is_captured())
return;
// Jetpack
if (bl_double)
{
p->jet_on();
bl_double = FALSE;
}
// Do movement
if (p->is_walking())
{
if (half_left)
p->walk_turn_left();
else if (half_right)
p->walk_turn_right();
if (confirmed_left)
p->walk_left();
else if (confirmed_right)
p->walk_right();
else
p->walk_h_halt();
if (confirmed_up)
p->jump();
else if (is_down)
p->duck();
}
else if (p->is_ducked())
{
if (!is_down)
p->stand();
if (half_left)
p->duck_left();
else if (half_right)
p->duck_right();
}
else if (p->is_jumping())
{
if (half_left)
p->jump_turn_left();
else if (half_right)
p->jump_turn_right();
if (confirmed_left)
p->jump_left();
else if (confirmed_right)
p->jump_right();
else
p->jump_h_halt();
}
else if (p->is_jetting())
{
if (confirmed_up)
p->jet_up();
else if (is_down)
p->jet_off();
else
p->jet_v_halt();
if (half_left)
p->jet_turn_left();
else if (half_right)
p->jet_turn_right();
if (confirmed_left)
p->jet_left();
else if (confirmed_right)
p->jet_right();
else
p->jet_h_halt();
}
else if (p->is_climbing())
{
if (confirmed_up)
p->climb_up();
else if (confirmed_down)
p->climb_down();
else
p->climb_v_halt();
if (half_left)
p->climb_turn_left();
else if (half_right)
p->climb_turn_right();
if (confirmed_left)
p->climb_left();
else if (confirmed_right)
p->climb_right();
else
p->climb_h_halt();
}
}